package com.situ.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.mapper.AdminMapper;
import com.situ.mall.pojo.Admin;
import com.situ.mall.mapper.AdminMapper;
import com.situ.mall.pojo.Admin;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2025-06-17
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public PageResult list(AdminQuery adminQuery) {
        // select * from admin where name='zhang' and create_time between xxx and xxx limit 0,10;
        IPage<Admin> page = new Page<>(adminQuery.getPage(), adminQuery.getLimit());
        //查询条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(adminQuery.getName()), "name", adminQuery.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(adminQuery.getEmail()), "email", adminQuery.getEmail());
        queryWrapper.eq(!ObjectUtils.isEmpty(adminQuery.getRole()), "role", adminQuery.getRole());
        queryWrapper.between(!ObjectUtils.isEmpty(adminQuery.getBeginCreateTime()) && !ObjectUtils.isEmpty(adminQuery.getEndCreateTime()),
                "create_time", adminQuery.getBeginCreateTime(), adminQuery.getEndCreateTime() );
        adminMapper.selectPage(page, queryWrapper);

        return new PageResult(0, "", (int)page.getTotal(), page.getRecords());
    }

    @Override
    public Admin login(String name, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("password", password);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }
}
